package com.telnetnz.ticketdesk.dao.wrapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import com.rockbb.common.dao.CacheManagerBean;
import com.telnetnz.ticketdesk.dao.entity.TicketEntity;
import com.telnetnz.ticketdesk.dao.mapper.TicketMapper;

public class TicketWrap {

	public static final String[] ORDER_BY = { 
		"ticket_id", 
		"ticket_priority",
		"due_date",
		"create_time",
		"update_time",
		"ticket_status"
	};
	public static final String[] ORDER = {"DESC", "ASC"};
	public static final String[] TYPES = {"bug", "support", "new feature"};
	public static final String[] PRIORITIES = {"Normal", "High", "Urgent"};
	public static final String[] STATUSES = {"New", "Progress", "Closed", "Canceled"};

	private TicketMapper mapper;
	private Cache cache;

	public TicketWrap() {}

	public TicketWrap(
			TicketMapper mapper, 
			CacheManagerBean cacheManagerBean) {
		this.mapper = mapper;
		this.cache = cacheManagerBean.get(mapper.getClass().getName());
	}

	public void updateBatchToProjectId(int sourceProjectId, int targetProjectId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("project_id", sourceProjectId);
		parameters.put("target_id", targetProjectId);
		mapper.updateBatchToProjectId(parameters);
	}

	public TicketEntity select(long id) {
		return mapper.select(id);
	}

	public long countAll() {
		return mapper.countAll();
	}

	public void insert(TicketEntity entity) {
		mapper.insert(entity);
	}

	public void update(TicketEntity entity) {
		mapper.update(entity);
	}

	public void delete(TicketEntity entity){
		mapper.delete(entity);
	}

	public void deleteByPrimaryKey(long id) {
		mapper.deleteByPrimaryKey(id);
	}

	public List<Long> selectByPageAndCondition(
			int start,
			int limit, 
			int orderby, 
			int order,
			List<Integer>  projectIds,
			long extraCreatorId,
			long memberId,
			List<Long> ownerIds,
			long creatorId,
			long updaterId,
			String subject,
			String content,
			int priority,
			int type,
			List<Integer> statuses,
			List<Integer> excludedStatuses,
			long tagId,
			Date dueAfter,
			Date dueBefore,
			Date createAfter,
			Date createBefore,
			Date updateAfter,
			Date updateBefore)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (start < 0) {parameters.put("start", 0);}
		else {parameters.put("start", start);}

		if (limit <=0) {parameters.put("limit", 20);}
		else {parameters.put("limit", limit);}

		if (orderby > ORDER_BY.length - 1 || orderby < 0) {parameters.put("orderby", ORDER_BY[0]);}
		else {parameters.put("orderby", ORDER_BY[orderby]);}

		parameters.put("order", (order == 0) ? ORDER[0] : ORDER[1]);

		if (projectIds != null && projectIds.size() > 0) 
		{
			parameters.put("project_ids", projectIds);
			parameters.put("extra_creator_id", extraCreatorId);
		}
		if (memberId > 0L) {parameters.put("member_id", memberId);}
		if (ownerIds != null && ownerIds.size() > 0) {parameters.put("owner_ids", ownerIds);}
		if (creatorId > 0L) {parameters.put("creator_id", creatorId);}
		if (updaterId > 0L) {parameters.put("updater_id", updaterId);}
		if (subject != null && subject.length() > 0) {parameters.put("subject", '%'+subject+'%');}
		if (content != null && content.length() > 0) {parameters.put("content", '%'+content+'%');}
		if (priority > -1) {parameters.put("ticket_priority", priority);}
		if (type > -1) {parameters.put("ticket_type", type);}
		if (statuses != null && statuses.size() > 0) {parameters.put("ticket_statuses", statuses);}
		if (excludedStatuses != null && excludedStatuses.size() > 0) {parameters.put("excluded_statuses", excludedStatuses);}
		if (tagId > 0L) {parameters.put("tag_id", tagId);}
		if (dueAfter != null) parameters.put("due_after", dueAfter);
		if (dueBefore != null) parameters.put("due_before", dueBefore);
		if (createAfter != null) parameters.put("create_after", createAfter);
		if (createBefore != null) parameters.put("create_before", createBefore);
		if (updateAfter != null) parameters.put("update_after", updateAfter);
		if (updateBefore != null) parameters.put("update_before", updateBefore);

		return mapper.selectByPageAndCondition(parameters);
	}

	public long countByCondition(
			List<Integer>  projectIds,
			long extraCreatorId,
			long memberId,
			List<Long> ownerIds,
			long creatorId,
			long updaterId,
			String subject,
			String content,
			int priority,
			int type,
			List<Integer> statuses,
			List<Integer> excludedStatuses,
			long tagId,
			Date dueAfter,
			Date dueBefore,
			Date createAfter,
			Date createBefore,
			Date updateAfter,
			Date updateBefore)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (projectIds != null && projectIds.size() > 0) 
		{
			parameters.put("project_ids", projectIds);
			parameters.put("extra_creator_id", extraCreatorId);
		}
		if (memberId > 0) {parameters.put("member_id", memberId);}
		if (ownerIds != null && ownerIds.size() > 0) {parameters.put("owner_ids", ownerIds);}
		if (creatorId > 0) {parameters.put("creator_id", creatorId);}
		if (updaterId > 0) {parameters.put("updater_id", updaterId);}
		if (subject != null && subject.length() > 0) {parameters.put("subject", '%'+subject+'%');}
		if (content != null && content.length() > 0) {parameters.put("content", '%'+content+'%');}
		if (priority > -1) {parameters.put("ticket_priority", priority);}
		if (type > -1) {parameters.put("ticket_type", type);}
		if (statuses != null && statuses.size() > 0) {parameters.put("ticket_statuses", statuses);}
		if (excludedStatuses != null && excludedStatuses.size() > 0) {parameters.put("excluded_statuses", excludedStatuses);}
		if (tagId > 0L) {parameters.put("tag_id", tagId);}
		if (dueAfter != null) parameters.put("due_after", dueAfter);
		if (dueBefore != null) parameters.put("due_before", dueBefore);
		if (createAfter != null) parameters.put("create_after", createAfter);
		if (createBefore != null) parameters.put("create_before", createBefore);
		if (updateAfter != null) parameters.put("update_after", updateAfter);
		if (updateBefore != null) parameters.put("update_before", updateBefore);

		return mapper.countByCondition(parameters);
	}

	public Cache getCache() 
	{
		return cache;
	}
}

